package com.cs2340.taskplusplus;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import android.widget.TextView;


/**
 * Inserts each task from a tasklist into a viewable interface.
 * Is also a listener for each of the Views therein.
 * @author GRILLENBANNER
 *
 */
public class TaskListScreen extends Activity implements OnClickListener, OnCheckedChangeListener{

	private LinearLayout layout;
	private TaskList t;
	private TextView e;
	private Doable doable;
	private String name, tasklistname;
	private CheckBox c;
	private LinearLayout horizLayout, vertLayout;
	private int listIndex;
	private Intent intent;
	//filtering related variables
	private Sorter.FILTER sortMode = Sorter.FILTER.ALL;
	private Sorter.DATEMODE dateMode;
	private Date filterByDate;
	private Sorter.DONEMODE doneMode;
	private List<Label> filterByLabels;
	private List<Doable> displayList; //list of tasks to actually be displayed
	private PopupWindow pw;
	private View pview;

	
	/**
	 * onCreate method.
	 * Creates TaskList from database, sets listeners
	 * @param b The bundle associated with the new activity
	 */
	public void onCreate(Bundle b)
	{
		
		super.onCreate(b);
		setContentView(R.layout.tasklistview);
		
		
		//display listname
		name = this.getIntent().getExtras().getString("Task List Name");
		//TextView namefield = (TextView)findViewById(R.id.tasklistview_taskname);
		//namefield.setText(name);
		
		
		filterList();
		constructList();
        View addTask = findViewById(R.id.newTaskButton);
        addTask.setOnClickListener(this);
        View filterButton = findViewById(R.id.filterButton);
        filterButton.setOnClickListener(this);

        //View mapButton = findViewById(R.id.mapButton);
        //mapButton.setOnClickListener(this);
        
        //popup stuff
        LayoutInflater inflater = (LayoutInflater)
	       this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		pview = inflater.inflate(R.layout.filterpopupview, null, false);
		pw = new PopupWindow(
		       pview, 
		       100, 
		       100, 
		       true);
		
		//NUMBERS HERE ARE SET TO MAKE THE NEW XML LOOK GOOD (on my phone at least).
		//COULDNT FIGURE ANOTHER WAY TO DO THIS, KINDA KLUDGEY, HARD NUMBERS.
		// -CHARLES
		pw.setWidth(450);
		pw.setHeight(700);        
		
        //pw.setContentView(findViewById(R.layout.filterpopupview));
        filterButton = pview.findViewById(R.id.filterComplete);
        filterButton.setOnClickListener(this);
        filterButton = pview.findViewById(R.id.filterIncomplete);
        filterButton.setOnClickListener(this);
        filterButton = pview.findViewById(R.id.filterAll);
        filterButton.setOnClickListener(this);
        filterButton = pview.findViewById(R.id.filterLabel);
        filterButton.setOnClickListener(this);
        filterButton = pview.findViewById(R.id.filterDateAfter);
        filterButton.setOnClickListener(this);
        filterButton = pview.findViewById(R.id.filterDateBefore);
        filterButton.setOnClickListener(this);
        filterButton = pview.findViewById(R.id.filterDateOn);
        filterButton.setOnClickListener(this);       
		  
	}
	
	/**
	 * listens for the user clicking buttons and responds appropriately
	 * @param v The view clicked.
	 */
	public void onClick(View v)
	{	
		EditText e;
		
		
		
    	switch(v.getId())
    	{
    		/*
    		case (R.id.mapButton):
    			intent = new Intent(this, MapViewDisplay.class);
    			startActivity(intent);
    			break;
    		*/
    		case (R.id.newTaskButton):
				intent = new Intent(this, NewTaskScreen.class);
				intent.putExtra("Task List Name", name);
				startActivity(intent);
				break;
				
    		case (R.id.filterButton):
     			pw.showAtLocation(this.findViewById(R.id.tasklistview_main), Gravity.CENTER, 0, 0);
    		    break;
    		case (R.id.filterDateAfter):
    			sortMode = Sorter.FILTER.DATE;
				dateMode = Sorter.DATEMODE.AFTER;
				DatePicker dp = (DatePicker) pview.findViewById(R.id.datePicker_filterView);
				filterByDate = new Date(dp.getYear() - 1900, dp.getMonth(), dp.getDayOfMonth());
				filterList();
				constructList();
				pw.dismiss();
				break;
    		case (R.id.filterDateBefore):
    			sortMode = Sorter.FILTER.DATE;
				dateMode = Sorter.DATEMODE.BEFORE;
				dp = (DatePicker) pview.findViewById(R.id.datePicker_filterView);
				filterByDate = new Date(dp.getYear() - 1900, dp.getMonth(), dp.getDayOfMonth());
				filterList();
				constructList();
				pw.dismiss();
    			break;
    		case (R.id.filterDateOn):
    			sortMode = Sorter.FILTER.DATE;
				dateMode = Sorter.DATEMODE.ON;
				dp = (DatePicker) pview.findViewById(R.id.datePicker_filterView);
				filterByDate = new Date(dp.getYear() - 1900, dp.getMonth(), dp.getDayOfMonth());
				filterList();
				constructList();
				pw.dismiss();
    			break;
    		case (R.id.filterComplete):
    			sortMode = Sorter.FILTER.COMPLETE;
				doneMode = Sorter.DONEMODE.COMPLETE;
				filterList();
				constructList();
				pw.dismiss();
    			break;
    		case (R.id.filterIncomplete):
    			sortMode = Sorter.FILTER.COMPLETE;
    			doneMode = Sorter.DONEMODE.INCOMPLETE;
    			filterList();
    			constructList();
				pw.dismiss();
    			break;
    		case (R.id.filterAll):
    			sortMode = Sorter.FILTER.ALL;
				filterList();
				constructList();
				pw.dismiss();
    			break;
    		case (R.id.filterLabel):
				e = (EditText) pview.findViewById(R.id.labelFilter);
    			sortMode = Sorter.FILTER.LABEL;
    			filterByLabels = new ArrayList<Label>();
    			filterByLabels.add(new Label(t.getTableName(), e.getText().toString()));
    			filterList();
    			constructList();
				pw.dismiss();
    			break;
			default:
				Doable tsk = (Doable) displayList.get(v.getId());
				if (tsk.isList())
				{
					TaskList tsk2 = (TaskList) tsk;
					intent = new Intent(this, TaskListScreen.class);
					intent.putExtra("Task List Name", tsk2.getTableName());
					startActivity(intent);
				}
				else
				{
					intent = new Intent(this, TaskScreen.class);
					intent.putExtra("Task Name", tsk.getName());
					intent.putExtra("Task Parent Table", t.getTableName());
					startActivity(intent);
				}
				break;			
			
    	}
    	View p = (View) v.getParent();
    	if (p != null)
    	{
    		if (p.getId() == R.id.tasklistview_taskLayout)
    		{
    			TextView v2 = (TextView) v;
    			Intent i = new Intent(this, TaskScreen.class);
    			i.putExtra("Task Name", v2.getText());
    			i.putExtra("Task Parent Table", t.getTableName());
    			startActivity(i);
    		}
    	}
	}
	
	/**
	 * Listener for the checkboxes.
	 * @param c The checkbox
	 * @param isChecked
	 */
	public void onCheckedChanged(CompoundButton c, boolean isChecked){
		doable = t.getList().get(c.getId());
		if (isChecked) 
			doable.setDone();
		else 
			doable.setNotDone();
	}
	
	/**
	 * onRestart method for navigating back to the activity.
	 * Updates the list to reflect any changes that have happened.
	 */
	public void onRestart()
	{
		super.onRestart();
		filterList();
		constructList();
	}
	
	/**
	 * Create the list of tasks that the screen displays, according to its filter settings
	 */
	public void filterList(){
		//get the raw list of doables
		t = new TaskList(name);
		//apply the appropriate filter
		switch (sortMode){
			case ALL:
				displayList = t.getList();
				break;
			case COMPLETE:
				displayList = Sorter.filterComplete(t.getList(), doneMode);
				break;
			case DATE:
				displayList = Sorter.filterDate(t.getList(), filterByDate, dateMode);
				break;
			case LABEL:
				displayList = Sorter.filterLabel(t.getList(), filterByLabels);
				break;
		}
	}
	
	/**
	 * For each doable in the parent table, add a check box, the name, and description
	 * into the Activity
	 */
	public void constructList()
	{
		listIndex = 0;
		layout = (LinearLayout) findViewById(R.id.tasklistview_taskLayout);
		layout.removeAllViews();
		
		
		if (t.getList() != null)
		{
			vertLayout = new LinearLayout(this);
			if (t.getParentTable()==null) tasklistname = Account.username;
			else tasklistname = t.getName();
			vertLayout.addView(addTextView(tasklistname, 30, 90));
			vertLayout.setPadding(10,0,0,0);
			layout.addView(vertLayout);
			 
			for (Doable d : displayList)
			{
				horizLayout = new LinearLayout(this);
				vertLayout = new LinearLayout(this);
				vertLayout.setOrientation(LinearLayout.VERTICAL);
				vertLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
				horizLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
				c = new CheckBox(this);
				c.setOnCheckedChangeListener(this);
				c.setId(listIndex);
				c.setGravity(Gravity.RIGHT);
				if (d.isDone()){
					c.setChecked(true);
				}
				horizLayout.addView(c);
				
				if (d.isList()) vertLayout.addView(addTextView(d.getName()+" [sublist]", 24));
				else vertLayout.addView(addTextView(d.getName(), 24));
				vertLayout.addView(addTextView(d.getDescription(), 14));
				
				
				vertLayout.setId(listIndex);
				listIndex++;
				vertLayout.setClickable(true);
				vertLayout.setOnClickListener(this);
				
				horizLayout.addView(vertLayout);
				horizLayout.setPadding(0, 0, 0, 40);
				layout.addView(horizLayout);
			}
			layout.setPadding(0,0,0,50);
		}
	}
	
	
	/**
	 * adds a textView to the activity
	 * @param s The text to add
	 * @param textSize
	 * @return The created TextView
	 */
	public TextView addTextView(String s, int textSize)
	{
		return addTextView(s, textSize, 0);
	}
	
	/**
	 * Adds a text View to the activity
	 * @param s The text to add
	 * @param textSize 
	 * @param boxHeight
	 * @return The created TextView
	 */
	public TextView addTextView(String s, int textSize, int boxHeight)
	{
		e = new TextView(this);
		e.setLayoutParams(new LayoutParams(
	            LayoutParams.WRAP_CONTENT,
	            LayoutParams.WRAP_CONTENT));
		e.setTextSize(textSize);
		if (boxHeight != 0) e.setHeight(boxHeight);
		e.setText(s);
		/*
		if (isClickable)
		{
			e.setClickable(true);
		}
		*/
		return e;
	}
	
	//these are for debugging only
	public void setName (String name){
		this.name = name;
	}
	
	public void setSortMode (Sorter.FILTER sortMode){
		this.sortMode = sortMode;
	}
	
	public void setDateMode (Sorter.DATEMODE dateMode){
		this.dateMode = dateMode;
	}
	
	public void setDate (Date d){
		this.filterByDate = d;
	}
	
	public void setDoneMode (Sorter.DONEMODE doneMode){
		this.doneMode = doneMode;
	}
	
	public void setLabels (List<Label> labels){
		this.filterByLabels = labels;
	}
	
	public List<Doable> getDisplayList(){
		return this.displayList;
	}
	//end debugging methods
	
}
